package main.dao;

import main.view.Main;

import java.sql.*;
import java.text.SimpleDateFormat;

public class HistoryDB {
    public ResultSet readDate(String part) throws SQLException {
        System.out.println(part);
        String sql="select * from history where part=? and returnTime=?";
        String returnTime = "2022-07-10 00:00:00";
        Connection con = new Main().getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        ps.setString(1,part);
        ps.setString(2,returnTime);
        ResultSet rs=ps.executeQuery();
        return rs;
    }
    public void writeDate(String title, String part) throws SQLException {
        String sql="Insert into history (part,title,createTime,returnTime) values(?,?,?,?)";
        String returnTime = "2022-07-10 00:00:00";
        Connection con = new Main().getConnection();
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String createTime = sdf.format(date);
        PreparedStatement ps=con.prepareStatement(sql);
        ps.setString(1,part);
        ps.setString(2,title);
        ps.setString(3,createTime);
        ps.setString(4,returnTime);
        ps.executeUpdate();
    }
    public void writeDate(ResultSet rs,String part) throws SQLException {
        String sql = "update history set returnTime=? where returnTime=? and part=?";
        Connection con = new Main().getConnection();
        SimpleDateFormat sdf = new SimpleDateFormat();
        PreparedStatement ps=con.prepareStatement(sql);

        while(rs.next()){
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String momentTime = sdf.format(date);
            String returnTime = "2022-07-10 00:00:00";
            System.out.println(momentTime+returnTime);
            ps.setString(1,momentTime);
            ps.setString(2,returnTime);
            ps.setString(3,part);
            System.out.println(rs.getString(2));
            new BookStorageDB().refreshData(rs);
            ps.executeUpdate();
        }

    }
}
